package biz.datalk.industrialland.admin.modules.online.dynform.controller;

import biz.datalk.industrialland.admin.modules.online.dynform.pojo.dto.DynformAutoDTO;
import biz.datalk.industrialland.admin.modules.online.dynform.pojo.vo.DynformFieldVo;
import biz.datalk.industrialland.admin.modules.online.dynform.service.IDynfromAutoApiService;
import biz.datalk.industrialland.common.encrypt.annotation.RSAES;
import biz.datalk.industrialland.common.json.annotation.Json;
import biz.datalk.industrialland.common.result.UnifyResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 *
 *
 * @author tarofang@163.com
 * @date 2020-04-07
 *
 */
@Slf4j
@RestController
@RequestMapping("/api/dynform")
public class DynformAutoApiController {

    @Autowired
    private IDynfromAutoApiService dynfromAutoApiService;

    @RSAES
    @PostMapping(value = "/getItems", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Json(type = DynformFieldVo.class, include = "fieldName,fieldComment,fieldContent,files,fieldLength,pageShowType,pageShowTypeLength,pageMustInput,dicts,fileMaxNum,imgPreviewPath,pageContentHint,children,groupId,groupName,fieldDefault")
    public UnifyResult getItems(@RequestBody DynformAutoDTO dynformAutoDTO) {
        String tableId = StringUtils.trimToEmpty(dynformAutoDTO.getTableId());
        String id = StringUtils.trimToEmpty(dynformAutoDTO.getId());
        if (StringUtils.isBlank(tableId)) {
            return UnifyResult.fail("请求参数错误");
        }
        boolean needGroup = StringUtils.equals("Y", dynformAutoDTO.getNeedGroup());

        List<DynformFieldVo> resList = dynfromAutoApiService.getItems(tableId, id, needGroup);
        return UnifyResult.success().setData(resList);
    }

    @RSAES
    @PostMapping(value = "/submit", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public UnifyResult submit(@RequestBody DynformAutoDTO dynformAutoDTO) {
        return dynfromAutoApiService.submit(dynformAutoDTO);
    }

}
